<template>
  <div>
  <desc-title
  :descTitle=descTitle
  >
  </desc-title>
  <div class="content">
    <el-form :model="addSensorParams" :rules="rules" ref="addSensorParams" label-width="300px">
        <el-form-item label="场景类型名称：" prop="sceneTypeName">
          <el-input type="text" placeholder="请输入场景类型名称"  v-model="addSensorParams.sceneTypeName" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="场景描述：" prop="sceneTypeDesc">
          <el-input
            type="textarea"
            placeholder="请输入部门描述"
            v-model="addSensorParams.sceneTypeDesc"
            maxlength="50"
            show-word-limit
            :rows="8"
            style="width:560px"
          ></el-input>
        </el-form-item>
        <el-form-item label="关联传感器类型：">
          <el-select v-model="showSensorListName" placeholder="请选择传感器类型">
            <el-option
              v-for="item in sensorInfo"
              :key="item.deviceTypeId"
              :value="item.deviceTypeName"
              :label="item.deviceTypeName"
              @click.native="selectSensor(item)"
              >
            </el-option>
          </el-select>
          <div class="add" @click="addSensorDevice">添加</div>
          <div class="DeviceType">
            <div class="DeviceTypeList" v-for="(item, index) in DeviceTypeListName" :key="index">
              <div class="DeviceTypeListName">{{ item.name }}</div>
              <i class="el-icon-close" @click="deleteDevice(item)"></i>
            </div>
          </div>
        </el-form-item>
    </el-form>
    <div class="addBtn">
      <span class="confirm flex-center" @click="determineSensor">确定</span>
    </div>
  </div>
</div>
</template>

<script>
import { addSceneType, getSensorSlectList } from '@/api/scene'
export default {
  data () {
    return {
      descTitle: '场景信息',
      sensorInfo: [],
      // 双向绑定选择的传感器
      showSensorListName: '',
      showSensorListId: '',
      // 是否展示下拉框
      ifShowSelectSensor: false,
      DeviceTypeListName: [],
      addSensorParams: {
        sceneTypeDesc: '',
        sceneTypeName: '',
        typeHead: '',
        deviceTypeIds: [],
        isValid: 0
      },
      rules: {
        sceneTypeName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '长度需小于10个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    // 获取传感器列表 下拉菜单的
    async getSensor () {
      const res = await getSensorSlectList()
      // console.log(res)
      this.sensorInfo = res.data.data
    },
    // 选择下拉菜单中的某一项
    selectSensor (item) {
      this.showSensorListName = item.deviceTypeName
      this.showSensorListId = item.deviceTypeId
      this.ifShowSelectSensor = false
    },
    // 添加设备的按钮
    addSensorDevice () {
      // console.log(this.showSensorListId)
      if (this.showSensorListName) {
        const result = this.DeviceTypeListName.some(item => {
          return item.id === this.showSensorListId
        })
        if (!result) {
          this.DeviceTypeListName.push({
            name: this.showSensorListName,
            id: this.showSensorListId
          })
        } else {
          this.$message({
            message: '已添加过相同传感器类型',
            type: 'error',
            duration: 1000
          })
        }
        this.showSensorListName = ''
      } else {
        this.$message({
          message: '请先选择需要添加的设备',
          type: 'error',
          duration: 1000
        })
      }
    },
    // 确定添加
    async determineSensor () {
      for (var i = 0; i < this.DeviceTypeListName.length; i++) {
        if (
          this.addSensorParams.deviceTypeIds.indexOf(
            this.DeviceTypeListName[i].id
          ) === -1
        ) {
          this.addSensorParams.deviceTypeIds.push(
            this.DeviceTypeListName[i].id
          )
        }
      }
      this.$refs.addSensorParams.validate(async valid => {
        if (valid) {
          const { data: res } = await addSceneType(this.addSensorParams)
          // console.log(res)
          if (res.code === 200) {
            this.$message({
              message: res.message,
              type: 'success',
              duration: 500
            })
            this.$router.push('/sceneManagement')
          } else if (res.code === 901) {
            this.$message({
              message: res.message,
              type: 'error',
              duration: 500
            })
          } else {
            this.$message({
              message: '请填写相关信息',
              type: 'error',
              duration: 500
            })
          }
        }
      })
    },
    // 删除传感器按钮
    deleteDevice (item) {
      this.DeviceTypeListName.forEach(item1 => {
        if (item1 === item) {
          const j = this.DeviceTypeListName.indexOf(item1)
          this.DeviceTypeListName.splice(j, 1)
          // console.log(this.DeviceTypeListName)
        }
      })
    }
  },
  created () {
    this.getSensor()
  }
}
</script>

<style lang="scss" scoped>
.content{
  position: relative;
  background-color: #fff;
  min-height: calc(100% - 201px);
  margin-bottom: 30px;
  ::v-deep .el-form{
    .el-input{
      width: 400px;
      .el-input__inner{
        height: 48px;
        line-height: 48px;
      }
    }
  .el-input.is-disabled .el-input__inner{
    background:rgba(248,248,248,1);
    border-radius:6px;
    border:1px solid rgba(220,220,220,1);
    color: #333333;
  }
  .el-form-item{
    margin-bottom: 32px;
    .el-form-item__content{
      position: relative;
    }
  }
  .el-form-item__label{
    width: 300px !important;
    text-align: right;
    }
  .el-form-item__error{
    left: 300px;
    line-height: 24px;
  }
  .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before{
          content: '*';
          color: #4868FF;
          margin-right: 4px;
    }
  }
 ::v-deep .el-upload--picture-card{
    width:390px;
    height:220px;
    background-color: #fff;
    .nopic{
      height: 100%;
      align-items: center;
      justify-content: center;
      position: relative;
      .addpics{
        line-height: 20px;
        height:20px;
        font-size:14px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:rgba(200,200,200,1);
      }
    }
          .pictips{
            line-height: 20px;
            position: absolute;
            top: 0;
            left: 586px;
            height:20px;
            font-size:14px;
            font-family:PingFangSC-Regular,PingFang SC;
            font-weight:400;
            color:rgba(255,180,21,1);
            cursor:default
          }
          .el-progress-circle{
            width:40px !important;
            height:40px !important;
          }
      }
    }
    .addBtn{
      padding: 150px 0 30px 300px ;
      .confirm{
        width:172px;
        height:48px;
        background:rgba(72,104,255,1);
        border-radius:24px;
        font-size:18px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:rgba(255,255,255,1);
        cursor: pointer;
      }
    }
    .add {
        position: absolute;
        top: 6px;
        left: 716px;
        cursor: pointer;
        width: 84px;
        height: 36px;
        background: rgba(72, 104, 255, 1);
        border-radius: 18px;
        line-height: 36px;
        text-align: center;
        font-size: 14px;
        color: rgba(255, 255, 255, 1);
      }
    .DeviceType {
      margin: 20px 0 0 300px;
      width: 560px;
      height: 240px;
      border-radius: 5px;
      border: 1px solid rgba(230, 230, 230, 1);
      .DeviceTypeList {
        float: left;
        height: 36px;
        border: 1px solid rgba(242, 242, 242, 1);
        margin-left: 12px;
        margin-top: 12px;
        .DeviceTypeListName {
          font-size: 14px;
          float: left;
          line-height: 36px;
          padding-left: 10px;
        }
        .el-icon-close {
          cursor: pointer;
          padding-left: 24px;
          float: left;
          font-size: 15px;
          line-height: 36px;
          padding-right: 12px;
        }
      }
      .DeviceTypeList:hover{
                border-color: #FF2B2B !important;
        .el-icon-close {
          color:#FF2B2B !important;
        }
      }
    }
</style>
